package org.kongqian.gift.exception;

import org.kongqian.gift.exception.code.ErrorCode;

import java.util.List;

public class AssertionException {

    //为空就报错
    public static void assertionNotNull(Object obj,String message){
        if (obj == null){
            throw new GlobalException(message);
        }
    }


    //不为空就报错
    public static void assertionNull(Object obj,String message){
        if (obj != null){
            throw new GlobalException(message);
        }
    }

    //不为空就报错
    public static void assertionNull(Object obj, ErrorCode code){
        if (obj != null){
            throw new GlobalException(code.getMessage(),code.getCode());
        }
    }

    //字符串为空就报错
    public static void assertionNotBlank(String str,String message){
        if (str == null || str.trim().equals("")){
            throw new GlobalException(message);
        }
    }

    //字符串为空就报错
    public static void assertionNotBlank(String str,ErrorCode code){
        if (str == null || str.trim().equals("")){
            throw new GlobalException(code.getMessage(),code.getCode());
        }
    }

    //字符串不为空就报错
    public static void assertionBlank(String str,String message){
        if (str != null || !str.trim().equals("")){
            throw new GlobalException(message);
        }
    }

    //集合为空就报错
    public static void assertionListNotNull(List<?> list, ErrorCode code){
        if (list == null || list.size() <1 ){
            throw new GlobalException(code.getMessage(),code.getCode());
        }
    }

    //集合为空就报错
    public static void assertionListNotEq(List<?> list, Integer integer,ErrorCode code){
        if (list.size() != integer.intValue()){
            throw new GlobalException(code.getMessage(),code.getCode());
        }
    }

    //集合不为空就报错
    public static void assertionListNull(List<?> list, String message){
        if (list != null || list.size() >0 ){
            throw new GlobalException(message);
        }
    }

    //集合不为空就报错
    public static void assertionListNull(List<?> list, ErrorCode code){
        if (list != null && list.size() >0 ){
            throw new GlobalException(code.getMessage(),code.getCode());
        }
    }

    //str不相等就报错
    public static void assertionStrEq(String str1, String str2,String message){
        assertionNotBlank(str1,message);
        assertionNotBlank(str2,message);

        if (!str1.equals(str2)){
            throw new GlobalException(message);
        }
    }

    //str不相等就报错
    public static void assertionStrEq(String str1, String str2,ErrorCode code){
        assertionNotBlank(str1,code);
        assertionNotBlank(str2,code);

        if (!str1.equals(str2)){
            throw new GlobalException(code.getMessage(),code.getCode());
        }
    }
}
